package com.yuetong.inventory.service.impl;

import java.util.List;
import com.yuetong.common.utils.DateUtils;
import com.yuetong.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.yuetong.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.yuetong.inventory.domain.YtInventoryHistory;
import com.yuetong.inventory.mapper.YtInventoryMapper;
import com.yuetong.inventory.domain.YtInventory;
import com.yuetong.inventory.service.IYtInventoryService;

/**
 * 库存Service业务层处理
 *
 * @author  卓晓君
 * @date 2025-03-21
 */
@Service
public class YtInventoryServiceImpl implements IYtInventoryService
{
    @Autowired
    private YtInventoryMapper ytInventoryMapper;

    /**
     * 查询库存
     *
     * @param inventoryId 库存主键
     * @return 库存
     */
    @Override
    public YtInventory selectYtInventoryByInventoryId(Long inventoryId)
    {
        return ytInventoryMapper.selectYtInventoryByInventoryId(inventoryId);
    }

    /**
     * 查询库存列表
     *
     * @param ytInventory 库存
     * @return 库存
     */
    @Override
    public List<YtInventory> selectYtInventoryList(YtInventory ytInventory)
    {
        return ytInventoryMapper.selectYtInventoryList(ytInventory);
    }

    /**
     * 新增库存
     *
     * @param ytInventory 库存
     * @return 结果
     */
    @Transactional
    @Override
    public int insertYtInventory(YtInventory ytInventory)
    {
        ytInventory.setCreateTime(DateUtils.getNowDate());
        ytInventory.setCreateBy(SecurityUtils.getLoginUser().getUsername());
        int rows = ytInventoryMapper.insertYtInventory(ytInventory);
        insertYtInventoryHistory(ytInventory);
        return rows;
    }

    /**
     * 修改库存
     *
     * @param ytInventory 库存
     * @return 结果
     */
    @Transactional
    @Override
    public int updateYtInventory(YtInventory ytInventory)
    {
        ytInventory.setUpdateTime(DateUtils.getNowDate());
        ytInventory.setUpdateBy(SecurityUtils.getLoginUser().getUsername());
        ytInventoryMapper.deleteYtInventoryHistoryByInventoryId(ytInventory.getInventoryId());
        insertYtInventoryHistory(ytInventory);
        return ytInventoryMapper.updateYtInventory(ytInventory);
    }

    /**
     * 批量删除库存
     *
     * @param inventoryIds 需要删除的库存主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtInventoryByInventoryIds(Long[] inventoryIds)
    {
        ytInventoryMapper.deleteYtInventoryHistoryByInventoryIds(inventoryIds);
        return ytInventoryMapper.deleteYtInventoryByInventoryIds(inventoryIds);
    }

    /**
     * 删除库存信息
     *
     * @param inventoryId 库存主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteYtInventoryByInventoryId(Long inventoryId)
    {
        ytInventoryMapper.deleteYtInventoryHistoryByInventoryId(inventoryId);
        return ytInventoryMapper.deleteYtInventoryByInventoryId(inventoryId);
    }

    /**
     * 新增出入库记录信息
     *
     * @param ytInventory 库存对象
     */
    public void insertYtInventoryHistory(YtInventory ytInventory)
    {
        List<YtInventoryHistory> ytInventoryHistoryList = ytInventory.getYtInventoryHistoryList();
        Long inventoryId = ytInventory.getInventoryId();
        if (StringUtils.isNotNull(ytInventoryHistoryList))
        {
            List<YtInventoryHistory> list = new ArrayList<YtInventoryHistory>();
            for (YtInventoryHistory ytInventoryHistory : ytInventoryHistoryList)
            {
                ytInventoryHistory.setInventoryId(inventoryId);
                list.add(ytInventoryHistory);
            }
            if (list.size() > 0)
            {
                ytInventoryMapper.batchYtInventoryHistory(list);
            }
        }
    }
}
